package com.yiwei.zxp.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Spanned;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.bumptech.glide.Glide;
import com.yiwei.zxp.R;
import com.yiwei.zxp.activity.BussinessOrderDetailActivity;
import com.yiwei.zxp.api.model.SellOrderModel;
import com.yiwei.zxp.util.Arith;
import com.yiwei.zxp.util.Spans;
import com.yiwei.zxp.util.UI;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class BussinessOrderAdapter extends ItemAdapter<SellOrderModel.DatasBean, BussinessOrderAdapter.DefaultHolder> {

    private final int x24;
    private final int x36;

    private Context context;

    public BussinessOrderAdapter(Context context) {
        super(context);
        this.context = context;
        x24 = context.getResources().getDimensionPixelSize(R.dimen.x24);
        x36 = context.getResources().getDimensionPixelSize(R.dimen.x36);
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_bussiness_order, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        SellOrderModel.DatasBean item = getItem(position);
        holder.item = item;

        holder.txtName.setText("订单号:" + item.order_info.order_no);
        if (item.items.size() == 1) {
            holder.layoutSingle.setVisibility(View.VISIBLE);
            holder.listImage.setVisibility(View.GONE);
            holder.txtGoodsTitle.setText(item.items.get(0).title);
            holder.txtGoodsNum.setText("x" + item.items.get(0).num);
            holder.txtGoodsPrice.setText("￥" + item.items.get(0).price);
            if (item.items.get(0).photos != null && item.items.get(0).photos.size() != 0) {
                Glide.with(context).load(item.items.get(0).photos.get(0)).asBitmap().centerCrop().into(holder.imgGoods);
            }
        } else if (item.items.size() > 1) {
            holder.layoutSingle.setVisibility(View.GONE);
            holder.listImage.setVisibility(View.VISIBLE);
            OrderPhotoAdapter adapter = new OrderPhotoAdapter(context);
            adapter.setOrder_no(item.order_info.order_no);
            holder.listImage.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
            holder.listImage.setAdapter(adapter);
            List<String> photos = new ArrayList<>();
            for (int i = 0; i < item.items.size(); i++) {
                if (item.items.get(i).photos != null && item.items.get(i).photos.size() != 0) {
                    photos.add(item.items.get(i).photos.get(0));
                }
            }
            adapter.update(photos);

        }
        String totalMoney = Arith.add(Double.valueOf(item.order_info.money), Double.valueOf(item.order_info.deliver_fee_total)) + "";
        Spanned txt_info = Spans.with(context).font("共" + item.order_info.num + "件商品    合计", x24, 0xff000000).
                font("￥", x24, 0xffFF0030).style(Typeface.BOLD).
                font(totalMoney, x36, 0xffFF0030).style(Typeface.BOLD).
                build();
        holder.txtInfo.setText(txt_info);
//        （-2:付款金额不满足,-1：失效，0：待付款，1：已付款/待发货，2：已发货，3：待评论，4：已完成）
        if (item.order_info.status.equals("-1")) {
            holder.layoutBottom.setVisibility(View.GONE);
        } else {
            holder.layoutBottom.setVisibility(View.VISIBLE);
        }

        if (item.order_info.status.equals("0")) {
            holder.btnOne.setVisibility(View.VISIBLE);
            holder.btnTwo.setVisibility(View.GONE);
            holder.btnOne.setText("提醒买家付款");
            holder.txtStatus.setText("待付款");
        } else if (item.order_info.status.equals("1")) {
            holder.btnOne.setVisibility(View.VISIBLE);
            holder.btnTwo.setVisibility(View.GONE);
            holder.btnOne.setText("立即发货");
            holder.txtStatus.setText("待发货");
        } else if (item.order_info.status.equals("2")) {
            holder.btnOne.setVisibility(View.VISIBLE);
            holder.btnTwo.setVisibility(View.VISIBLE);
            holder.btnOne.setText("查看物流");
            holder.btnTwo.setText("提醒买家收货");
            holder.txtStatus.setText("已发货");
        } else if (item.order_info.status.equals("-1")) {
            holder.btnOne.setVisibility(View.GONE);
            holder.btnTwo.setVisibility(View.VISIBLE);
            holder.btnTwo.setText("删除订单");
            holder.txtStatus.setText("失效");
        } else if (item.order_info.status.equals("3")) {
            holder.btnOne.setVisibility(View.VISIBLE);
            holder.btnOne.setText("回复评价");
            holder.btnTwo.setVisibility(View.GONE);
            holder.txtStatus.setText("待评价");
        } else if (item.order_info.status.equals("4")) {
            holder.btnOne.setVisibility(View.VISIBLE);
            holder.btnOne.setText("查看评价");
            holder.btnTwo.setVisibility(View.GONE);
            holder.txtStatus.setText("已完成");
        }

    }


    class DefaultHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.txt_name)
        TextView txtName;
        @BindView(R.id.txt_status)
        TextView txtStatus;
        @BindView(R.id.img_goods)
        ImageView imgGoods;
        @BindView(R.id.txt_goods_title)
        TextView txtGoodsTitle;
        @BindView(R.id.txt_goods_price)
        TextView txtGoodsPrice;
        @BindView(R.id.txt_goods_num)
        TextView txtGoodsNum;
        @BindView(R.id.list_image)
        RecyclerView listImage;
        @BindView(R.id.txt_info)
        TextView txtInfo;
        @BindView(R.id.btn_one)
        SuperButton btnOne;
        @BindView(R.id.btn_two)
        SuperButton btnTwo;
        @BindView(R.id.layout_single)
        RelativeLayout layoutSingle;
        @BindView(R.id.layout_bottom)
        LinearLayout layoutBottom;
        SellOrderModel.DatasBean item;

        @OnClick({R.id.btn_one, R.id.btn_two, R.id.list_image, R.id.layout_single})
        public void onViewClicked(View view) {
            if (UI.isFastClick()) {
                return;
            }
            switch (view.getId()) {
                case R.id.list_image:
                case R.id.layout_single:
                    Intent intent = new Intent(context, BussinessOrderDetailActivity.class);
                    intent.putExtra("order_no", item.order_info.order_no);
                    context.startActivity(intent);
                    break;
                case R.id.btn_one:
                    if (UI.toString(btnOne).equals("提醒买家付款")) {
                        if (onChooseListener != null) {
                            onChooseListener.onPay(item.order_info.order_no);
                        }
                    } else if (UI.toString(btnOne).equals("立即发货")) {
                        if (onChooseListener != null) {
                            onChooseListener.onSendTips(item.order_info.order_no);
                        }
                    } else if (UI.toString(btnOne).equals("查看物流")) {
                        if (onChooseListener != null) {
                            onChooseListener.onWuliu(item.order_info.order_no);
                        }
                    } else if (UI.toString(btnOne).equals("回复评价") || UI.toString(btnOne).equals("查看评价")) {
                        if (onChooseListener != null) {
                            onChooseListener.onComment();
                        }
                    }
                    break;
                case R.id.btn_two:
                    if (UI.toString(btnTwo).equals("提醒买家收货")) {
                        if (onChooseListener != null) {
                            onChooseListener.onSure(item.order_info.order_no);
                        }
                    } else if (UI.toString(btnTwo).equals("删除订单")) {
                        if (onChooseListener != null) {
                            onChooseListener.onDelete(item.order_info.order_no);
                        }
                    }
                    break;
            }
        }

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onPay(String order_no);

        void onSendTips(String order_no);

        void onSure(String order_no);

        void onWuliu(String order_no);

        void onDelete(String order_no);

        void onComment();
    }
}
